/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.springapplication.services.impl;

import cz.muni.fi.pa165.springapplication.dao.CustomerManager;
import cz.muni.fi.pa165.springapplication.entities.Customer;
import cz.muni.fi.pa165.springapplication.exceptions.IllegalOrphanException;
import cz.muni.fi.pa165.springapplication.exceptions.NonexistentEntityException;
import cz.muni.fi.pa165.springapplication.exceptions.PreexistingEntityException;
import cz.muni.fi.pa165.springapplication.services.CustomerService;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Daniel Sák, 374560
 */
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {   
    
    
    private CustomerManager cusManager;
    public void setCustomerManager(CustomerManager cusManager) {
        this.cusManager = cusManager;
    }
    
    @Override
    public void createCustomer(Customer customer) throws IllegalOrphanException, PreexistingEntityException {
        if(!isValidEntity(customer)){
            throw new IllegalArgumentException("Invalid customer entity.");
        }       
        cusManager.createCustomer(customer);   
    }

    @Override
    public void updateCustomer(Customer customer) throws NonexistentEntityException, IllegalOrphanException, PreexistingEntityException, Exception {
        if(!isValidEntity(customer)){
            throw new IllegalArgumentException("Invalid customer entity.");
        }        
        cusManager.updateCustomer(customer);       
    }

    @Override
    public void deleteCustomer(Customer customer) throws NonexistentEntityException, IllegalOrphanException {
       if(!isValidEntity(customer)){
            throw new IllegalArgumentException("Invalid customer entity.");
        } 
        cusManager.deleteCustomer(customer);
      
    }

    @Override
    public Customer getCustomerById(Long customerId) {
        return cusManager.getCustomerById(customerId);
    }

    
    private boolean isValidEntity(Customer customer){
        if(customer == null){
            return false;
        }
        if(customer.getFirstName() == null){
            return false;
        }
        
        if(customer.getSurName() == null){
            return false;
        }
        
        if(customer.getNumberOfIdCard() == null){
            return false;
        }
        
        return true;
    }
    

    
}

